package com.hjqjl.whdemo.utils;

import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import com.blankj.utilcode.util.LogUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextColorUtil {
    /**
     * 关键字高亮变色
     *
     * @param color   变化的色值
     * @param text    文字
     * @param keyword 文字中的关键字
     * @return
     */
    public static SpannableString findSearch(int color, String text, String keyword) {
        keyword = escapeExprSpecialWord(keyword);
        text = escapeExprSpecialWord(text);
        SpannableString s = new SpannableString(text);
        try {
            Pattern p = Pattern.compile(keyword);
            Matcher m = p.matcher(s);
            while (m.find()) {
                int start = m.start();
                int end = m.end();
                s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        } catch (Exception e) {
            LogUtils.v("字体颜色转换出错");
        }
        return s;
    }

    /**
     * 多个关键字高亮变色
     *
     * @param color   变化的色值
     * @param text    文字
     * @param keyword 文字中的关键字数组
     * @return
     */
    public static SpannableString findSearch(int color, String text, String... keyword) {
        text = escapeExprSpecialWord(text);
        SpannableString s = new SpannableString(text);
        try {
            for (int i = 0; i < keyword.length; i++) {
                keyword[i] = escapeExprSpecialWord(keyword[i]);
                Pattern p = Pattern.compile(keyword[i]);
                Matcher m = p.matcher(s);
                while (m.find()) {
                    int start = m.start();
                    int end = m.end();
                    s.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        } catch (Exception e) {
            LogUtils.v("字体颜色转换出错");
        }
        return s;
    }

    /**
     * 转义正则特殊字符 （$()*+.[]?\^{},|）
     * 防止特殊字符出错
     *
     * @param keyword
     * @return keyword
     */
    public static String escapeExprSpecialWord(String keyword) {
        if (!TextUtils.isEmpty(keyword)) {
            String[] fbsArr = {"\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|"};
            for (String key : fbsArr) {
                if (keyword.contains(key)) {
                    keyword = keyword.replace(key, "\\" + key);
                }
            }
        }
        return keyword;
    }

    /**
     * 通过Html.fromHtml方式修改颜色(解决SDK版本问题)
     *
     * @param html
     * @return
     */
    public static Spanned fromHtml(String html) {
        Spanned result;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            result = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
        } else {
            result = Html.fromHtml(html);
        }
        return result;
    }

    /**
     * 字体颜色修改
     *
     * @param content
     * @param color
     * @return
     */
    public static String color(String content, int color) {
        return "<font color=\"#" + Integer.toHexString(color) + "\" >" + content + "</font>";
    }

    /**
     * 加粗字体
     *
     * @param content
     * @return
     */
    public static String bold(String content) {
        return "<b>" + content + "</b>";
    }
}
